// 订单列表页
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userinfo:null,
    products:[],
    states:[
      { name: "全部", checked: true },
      { name: "已付款", checked: false },
      { name: "未付款", checked: false }
    ]
  },
  // 刷新页面
  change(){
    var m_id = app.globalData.m_id;
    var p = 1;
    var that = this
    wx.request({
      url: app.globalData.yuming + 'index.php/Api/Order/orderList',
      data: {
        m_id: m_id,
        p:p
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      success: function (res) {
        var mine = res.data.data;
        that.setData({
          products: mine
        })
      },
      fail: function () {

      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // 刷新页面，获取列表
   this.change();
  //  获的用户信息
  this.setData({
    userinfo:app.globalData.userInfo
  })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  // 取消订单
  cancel:function(e){
    var o_id = e.currentTarget.dataset.oid;
    var sure= this;
    wx.showModal({
      title:"提示",
      content:"确认取消此订单？",
      success:function(res){
        if (res.confirm) {
          sure.cancelNow(o_id);
        }
      }
    })
  },
  cancelNow:function(o_id){ 
    var that = this
    // 获取当前订单的o_id
    wx.request({
      url: app.globalData.yuming + 'index.php/Api/Order/cancelOrder',
      data:{
          o_id:o_id
      },
      header:{
          "content-type":"application/x-www-form-urlencoded"
      },
      method:"POST",
      success:function(res){
        var res = res.data.message
        wx.showModal({
          title:"提示",
          content:res,
          showCancel:false,
          success:function(res){
            if(res.confirm){
              that.change();
            }
          }
        })
      },
      fail:function(res){
        wx.showModal({
          title: "提示",
          content: res.data.message,
          showCancel: false,
          success: function (res) {
            if (res.confirm) {
              that.change();
            }
          }
        })
      }
    })
  },
  //选择订单状态
  check:function(e){
    // 切换按钮样式
    var index = e.currentTarget.dataset.idx;
    var states = this.data.states.map(function(state){
        state['checked'] = false
        return state
    })
    states[index]['checked'] = true
    this.setData({
      states:states
    })
    //筛选展示的内容
    if(index == 2){
      index = 0
      this.indexPro(index);
    }else if(index == 1){
      this.indexPro(index);
    }else{
      this.change()
    }
    
  },
  // 取到对应状态的订单显示
  indexPro:function(index){
    var that = this;
    var m_id = app.globalData.m_id;
    var p = 1;
    wx.request({
      url: app.globalData.yuming + 'index.php/Api/Order/orderList',
      data: {
        m_id: m_id,
        p:p
      },
      header: {
        "content-type": "application/x-www-form-urlencoded"
      },
      method: "POST",
      success: function (res) {
        console.log(res.data.data)
        var products = []
        console.log(index)
        var mine = res.data.data.map(function (product) {
          if (product['status'] == index) {
            products.push(product)
          }
          console.log(products)
        })
        that.setData({
          products: products
        })
      },
      fail: function (res) {
        console.log(res.data.message)
      }
    })
  },
  // 催单
  urge:function(e){
    var o_id = e.currentTarget.dataset.oid;
    wx.request({
      url: app.globalData.yuming + 'index.php/Api/Order/reminderOrder',
      data:{
        o_id:o_id
      },
      header:{
        "content-type":"application/x-www-form-urlencoded"
      },
      method:"POST",
      success:function(res){
        wx.showModal({
          title: '提示',
          content: res.data.message,
          showCancel:false
        })
      },
      fail:function(res){
        wx.showModal({
          title:"提示",
          content: res.data.message,
          showCancel:false
        })
      }
    })
  },
  // 删除订单
  delete:function(e){
    var that = this;
    var o_id = e.currentTarget.dataset.oid;
    wx.showModal({
      title: '提示',
      content: '确认删除此订单？',
      success:function(res){
        if(res.confirm){
          wx.request({
            url: app.globalData.yuming +'index.php/Api/Order/delOrder',
            data:{
                o_id:o_id
            },
            header:{
              "content-type":"application/x-www-form-urlencoded"
            },
            method:"POST",
            success:function(res){
              wx.showModal({
                title: '提示',
                content: res.data.message,
                showCancel:false
              })
              that.change()
            },
            fail:function(res){
              wx.showModal({
                title: '提示',
                content: res.data.message,
                showCancel: false
              })
            }
          })
        }
      }
    })
  },
  // 订单详情
  orderDetail:function(e){
     var o_id = e.currentTarget.dataset.oid;
  },
  pay:function(){
    // wx.requestPayment({
    //   timeStamp: '',
    //   nonceStr: '',
    //   package: '',
    //   signType: '',
    //   paySign: '',
    // })
    console.log("去前台付款")
  }
})